import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from "./store";
import './plugins/element.js'
import "./assets/css/base.scss"
import './assets/fonts/iconfont.css'
import * as echarts from 'echarts'

//导入进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import {postRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {initMenu} from "./utils/menus";


//导入富文本编辑器
import VueQuillEditor from 'vue-quill-editor';
import * as Quill from 'quill'
//导入富文本编辑器的样式
import 'quill/dist/quill.core.css'; // import styles
import 'quill/dist/quill.snow.css'; // for snow theme
import 'quill/dist/quill.bubble.css'; // for bubble theme

Vue.config.productionTip = false;
Vue.prototype.$echarts = echarts;

//将富文本编辑器注册为全局可用的组件
Vue.use(VueQuillEditor)


//插件
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;


Vue.filter('dateFormat',function(originVal){
  const dt = new Date(originVal)
  
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2,'0')
  const d = (dt.getDate() + '').padStart(2,'0')

  const hh = (dt.getHours() + '').padStart(2,'0')
  const mm = (dt.getMinutes() + '').padStart(2,'0')
  const ss = (dt.getSeconds() + '').padStart(2,'0')

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

//路由导航守卫
router.beforeEach((to, from, next) => {
  NProgress.start();
  // console.log(to.path);
  store.commit("initActivePath",to.path); // 初始化激活链接
  if (window.sessionStorage.getItem("tokenStr")) {
    initMenu(router, store);
    
    if( !window.sessionStorage.getItem("user")){
       return getRequest('/login/info').then( resp => {
         if(resp){
       
           //存入用户信息
           window.sessionStorage.setItem("user",JSON.stringify(resp));
           store.commit("initHomePath",resp.roles[0].id == 1 ? "/admin/home":"/teacher/home");
            next();
          }
       })
    }
    next();
  } else {
    if( to.path == "/"){
      next();
    }else{
      next('/?redirect=' + to.path);
    }
      
  }
})

router.afterEach(() => {
  NProgress.done()
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
